package com.canbot.u05.adapter;

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.canbot.u05.R;
import com.canbot.u05.bean.PersonData;
import com.canbot.u05.glide.GlideApp;
import com.canbot.u05.socket.IpMsgConst;
import com.canbot.u05.utils.PathConst;

import java.util.List;

public class AdminFaceChooseAdapter extends RecyclerView.Adapter<AdminFaceChooseAdapter.MyViewHolder> {

    private static final String TAG = "AdminFaceGroupAdapter";
    private Context mContext;
    private List<PersonData> personList;
    private OnItemClickListener listener;

    public AdminFaceChooseAdapter(Context context, List<PersonData> groupList) {
        this.personList = groupList;
        this.mContext = context;
    }

    public void setPersonList(List<PersonData> personList){
        this.personList = personList;
        notifyDataSetChanged();
    }

    public void setItemClickListener(OnItemClickListener listener){

        this.listener = listener;
    }

    @NonNull
    @Override
    public MyViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
        View view = LayoutInflater.from(mContext).inflate(R.layout.item_admin_face_choose, viewGroup,false);
        return new MyViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull MyViewHolder myViewHolder, int position) {

        Log.e(TAG,"PATH:" + personList.get(position).getPath());

        GlideApp.with(mContext).clear(myViewHolder.ivFace);
        GlideApp.with(mContext)
                .asDrawable()
                .load(IpMsgConst.HTTP_URL_CHEST + PathConst.FACES_ABS_DIR+ personList.get(position).getPath())
                .thumbnail(Glide.with(mContext).load(IpMsgConst.HTTP_URL_CHEST + PathConst.FACES_THUMB_DIR+ personList.get(position).getPath()))
                .diskCacheStrategy(DiskCacheStrategy.ALL)//缓存策略
                .skipMemoryCache(false)
                .placeholder(R.drawable.sales_banner01)//占位图
                .error(R.drawable.ic_picture_loadfailed)
                .override((int)mContext.getResources().getDimension(R.dimen.px290), (int)mContext.getResources().getDimension(R.dimen.px290))
                .into(myViewHolder.ivFace);
        myViewHolder.faceName.setText(personList.get(position).getName());
        myViewHolder.groupContainer.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (listener != null){
                    listener.onItemClick(personList.get(position));
                }
            }
        });
    }

    @Override
    public int getItemCount() {
        return personList.size();
    }

    public class MyViewHolder extends RecyclerView.ViewHolder {

        private TextView faceName;
        private ImageView ivFace;
        private RelativeLayout groupContainer;

        public MyViewHolder(@NonNull View itemView) {
            super(itemView);
            faceName = itemView.findViewById(R.id.tv_face_name);
            groupContainer = itemView.findViewById(R.id.rl_face_container);
            ivFace = itemView.findViewById(R.id.iv_face);
        }
    }

    public interface OnItemClickListener{
        void onItemClick(PersonData personData);
    }
}
